//
//  MusicMainViewController.m
//  One
//
//  Created by mac on 16/5/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MusicMainViewController.h"
#import <iCarousel/iCarousel.h>
#import "MusicViewWithTableViewController.h"
#import "HeadViewTableViewCell.h"
#import "MusicRequest.h"
#import "LookHistoryViewController.h"

@interface MusicMainViewController ()<iCarouselDelegate,iCarouselDataSource>

{
    MusicViewWithTableViewController *musicTable;
    NSArray *listArray;
    iCarousel *myCarousel;
    long int lastIndex;
    MusicModleObject *firstMicObj;
}

@end

@implementation MusicMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    lastIndex = 0;
    musicTable = [[MusicViewWithTableViewController alloc] init];
    listArray = [NSArray array];
    [NOTI_CENTER addObserver:self selector:@selector(requestHomePageData:) name:@"musicHomePageData" object:nil];
    
    myCarousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49)];
    myCarousel.pagingEnabled = YES;
    myCarousel.type = iCarouselTypeLinear;
    myCarousel.delegate = self;
    myCarousel.dataSource = self;
    myCarousel.bounceDistance = 0.2;
    myCarousel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myCarousel];
    
    [MusicRequest musicHomePageRequest];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return listArray.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    if (!view) {
        view = [[UIView alloc]initWithFrame:CGRectMake(0,64, WIDTH, HEIGHT - 113)];
        if(index == 0){
            UIView *backView = [[UIView alloc]initWithFrame:view.bounds];
            [view addSubview:backView];
            [MBProgressHUD showHUDAddedTo:backView animated:YES];
            [MusicRequest musicDetailRequestWithMusicId:[listArray[index] intValue] withBlock:^(MusicModleObject *obj) {
                musicTable.musicObject = obj;
                firstMicObj = obj;
                musicTable.currentPage = 0;
                [MBProgressHUD hideHUDForView:backView animated:YES];
                musicTable.view.frame = backView.bounds;
                musicTable.myTableView.frame = backView.bounds;
                [backView addSubview:musicTable.view];
              
            }];
        }
    }
    return view;
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel;
{
    if (carousel.currentItemIndex >= 0) {
        if (lastIndex == carousel.currentItemIndex) {
            return;
        }
        
        musicTable.view = nil;
        [MusicRequest musicDetailRequestWithMusicId:[listArray[carousel.currentItemIndex] intValue] withBlock:^(MusicModleObject *obj) {
            musicTable.musicObject = obj;
            musicTable.currentPage = carousel.currentItemIndex;
            musicTable.view.frame = carousel.currentItemView.bounds;
            musicTable.myTableView.frame = carousel.currentItemView.bounds;
            musicTable.currentPage = carousel.currentItemIndex;
            [carousel.currentItemView addSubview:musicTable.view];
        }];
    }
    lastIndex = carousel.currentItemIndex;
}

- (void)carouselDidScroll:(iCarousel *)carousel
{
    float draggedX;
        // 计算右拉的距离
    if(carousel.currentItemIndex == carousel.numberOfItems - 1 && carousel.currentItemIndex > 0)
    {
        draggedX = carousel.scrollOffset - carousel.currentItemIndex;
        if (draggedX == 0.2f) {
            
            NSArray *dataArr = [firstMicObj.maketime componentsSeparatedByString:@"-"];
            int year = [dataArr[0] intValue];
            int month = [dataArr [1] intValue];
            int countMonth = [self MonthGoesByWithCurrentYear:year CurrentMonth:month];
            
            LookHistoryViewController *hisVC = [[LookHistoryViewController alloc]init];
            hisVC.monthCount = countMonth;
            hisVC.currentYear = year;
            hisVC.currentMonth = month;
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:hisVC animated:YES];
        }
        
        NSLog(@"draggedX = %lf", draggedX);
    }
}

- (int)MonthGoesByWithCurrentYear:(int)year CurrentMonth:(int)month
{
    
    int goesMonth = (year - 2016)*12 + month;
    return goesMonth;
}

- (void)requestHomePageData:(NSNotification *)noti
{
    listArray = noti.object;
    [myCarousel reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
